package ccrm;
// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

import java.io.*;
import java.util.ArrayList;

import client.*;
import common.*;
import entitys.Request;
import boundarys.CreateProductGUI;
import boundarys.LoginGUI;

/**
 * This class constructs the UI for a client.  It implements the
 * interface in order to activate the display() method.
 * Warning: Some of the code here is cloned in ServerConsole 
 *
 * @author Fran&ccedil;ois B&eacute;langer
 * @author Dr Timothy C. Lethbridge  
 * @author Dr Robert Lagani&egrave;re
 * @version July 2000
 */
public class ClientMain  implements ConIF 
{
  //Class variables *************************************************
  
  /**
   * The default port to connect on.
   */
  final public static int DEFAULT_PORT = 5555;
  
  //Instance variables **********************************************
  
  /**
   * The instance of the client that created this Console.
   */
  public static CCRMClient client;
  
  LoginGUI login;
  
  //Constructors ****************************************************

  /**
   * Constructs an instance of the ClientConsole UI.
   *
   * @param host The host to connect to.
   * @param port The port to connect on.
   */
  public ClientMain(String host, int port) 
  {
    try 
    {
      client= new CCRMClient(host, port, this);
       login = new LoginGUI(host, port);
      } 
    catch(IOException exception) 
    {
      System.out.println("Error: Can't setup connection!"
                + " Terminating client.");
      System.exit(1);
    }
    
  }

  
  //Instance methods ************************************************
  
  /**
   * This method waits for input from the console.  Once it is 
   * received, it sends it to the client's message handler.
   */
  public static void accept(Request r) 
  {
	  
	  
    try
    {
    
           	client.handleMessageFromClientUI(r);	
      
    } 
    catch (Exception ex) 
    {
      System.out.println ("Unexpected error while reading from console!");
    }
  }

  /**
   * This method overrides the method in the ConIF interface.  It
   * displays a message onto the screen.
   *
   * @param message The string to be displayed.
   */
  public void display(String message) 
  {
    System.out.println("> " + message);
  }

  
  //Class methods ***************************************************
  
  /**
   * This method is responsible for the creation of the Client UI.
   *
   * @param args[0] The host to connect to.
   */
 // public static void main(String[] args)
  public static void connectClient(String[] args)
  {
    String host = "";
    int port = 0;  //The port number

    try
    {
      host = args[0];
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
      host = "localhost";
    }
    ClientMain con= new ClientMain(host, DEFAULT_PORT);
    if(con!=null)
    {
    	System.out.println("Connection to "+host+" succeeded!");
    //	System.out.println("Enter String-->Press \"Enter\"-->Enter \"Insert\" or \"Read\"--> Enjoy");
    	
   // 	con.accept();  //Wait for console data
    }
  } 


public static void connectClient(String args) {
	  String host = "";
	    int port = 0;  //The port number

	    try
	    {
	      host = args;
	    }
	    catch(ArrayIndexOutOfBoundsException e)
	    {
	      host = "localhost";
	    }
	    ClientMain con= new ClientMain(host, DEFAULT_PORT);
	    if(con!=null)
	    {
	    	System.out.println("Connection to "+host+" succeeded!");
	    //	System.out.println("Enter String-->Press \"Enter\"-->Enter \"Insert\" or \"Read\"--> Enjoy");
	    	
	   // 	con.accept();  //Wait for console data
	    }
	
}
}
//End of Console class
